#include<iostream>
#include<algorithm>
using namespace std;
const int N=20010;
struct node
{
	int l,r;
}nums[N];
int main()
{
	int n;cin>>n;
	for(int i=1;i<=n;i++)
	{
		cin>>nums[i].l>>nums[i].r;
	}
	sort(nums+1,nums+n+1,[](const node &a,const node& b)
	{
		return a.l<b.l;
	});
	int ret=0;
	int current_l=nums[1].l;
	int current_r=nums[1].r;
	for(int i=2;i<=n;i++)
	{
		int x=nums[i].l;int y=nums[i].r;
		if(current_r>=x)
		{
			current_r=max(y,current_r);
		}
		else
		{
			ret+=current_r-current_l;
			current_l=x;
			current_r=y;
		}
	}
	ret+=current_r-current_l;
	cout<<ret<<endl;
}